rm(list = setdiff(ls(), lsf.str()))

load("Study 1/Altered Data/Study1_UK_understanding.RData")

#SI A.1: Factor loadings----
subset_data<- data_UK[complete.cases(data_UK$total_populist, data_UK$w3_agre, data_UK$w3_open, data_UK$w3_con, data_UK$w3_ext, data_UK$w3_neu, data_UK$income, data_UK$w6_cynicism),]
CFA_agree <-'Agree = ~ NA*a1 + a2 + a3
Open = ~ NA*o1 + o2 + o3
Con = ~ NA*c1 + c2 + c3
Ext = ~ NA*e1 + e2 + e3
Neu = ~ NA*n1 + n2 + n3
Agree ~~ 1*Agree
Open ~~ 1*Open
Con ~~ 1*Con
Ext ~~ 1*Ext
Neu ~~ 1*Neu
' 

fit<-cfa(CFA_agree, data=data_UK)
fit<-cfa(CFA_agree, ordered=c(data_UK$a1, data_UK$a2, data_UK$a3, data_UK$o1, data_UK$o2, data_UK$o3, data_UK$c1, data_UK$c2, data_UK$c3, data_UK$e1, data_UK$e2, data_UK$e3, data_UK$n1, data_UK$n2, data_UK$n3), data=data_UK)
summary(fit, fit.measures=TRUE, standardized=T)
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[-c(16:65), ] 
names(p) <- c("Standardized Factor Loading", "p-value") 
(setattr(p, "row.names", c("Agreeableness item 1", "Agreeableness item 2", "Agreeableness item 3", "Openness item 1", "Openness item 2", "Openness item 3", "Conscientiousness item 1", "Conscientiousness item 2", "Conscientiousness item 3", "Extraversion item 1", "Extraversion item 2", "Extraversion item 3", "Neuroticism item 1", "Neuroticism item 2", "Neuroticism item3")))
p<-xtable(caption = "Big Five: Standardized Factor Loadings", label = "tab:Understanding_cfa", p)
print(p, type="latex", file="Tables/Understanding_agree_cfa.tex", caption.placement="top")

#SI A.1: Correlation matrix ---------
cor_ivs<-(with(data_UK, data.frame(data_UK$w3_agre, data_UK$w3_open, data_UK$w3_con, data_UK$w3_ext, data_UK$w3_neu, data_UK$w6_cynicism)))
names(cor_ivs) <- c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                    "4. Extraversion", "5. Neuroticism", 
                    "6. Cynicism")
correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- get_lower_tri(correlation.matrix)
correlation.matrix <- correlation.matrix[,c(1:5)]
names(correlation.matrix) <- seq(1,5,1)
stargazer(as.matrix(correlation.matrix), title="Understanding Society: Correlation Matrix of Independent Variables", font.size = "tiny", out="Tables/Understanding_cor.tex", no.space=TRUE, label="tab:Understanding_cor", digits=2)

#SI A.1: Results from regression model----------------
subset_data<-(with(data_UK, data.frame(total_populist, w3_agre , w3_open ,w3_con, w3_ext ,w3_neu ,female ,age , Ed_OtherQualification , Ed_GSCElevel, Ed_Alevel,Ed_higherdegree , Ed_degree, Ed_Missing, income ,  w6_cynicism)))
vote.UKIP<-list()
vote.UKIP[[1]]<- glm(total_populist ~zero1(w3_agre) + zero1(w3_open) + zero1(w3_con)+ zero1(w3_ext) + zero1(w3_neu) +female +age, subset_data, family=binomial)
vote.UKIP[[2]]<- glm(total_populist ~zero1(w3_agre) + zero1(w3_open) + zero1(w3_con)+ zero1(w3_ext) + zero1(w3_neu) +female +age + Ed_OtherQualification + Ed_GSCElevel + Ed_Alevel + Ed_higherdegree + Ed_degree + Ed_Missing + income +  zero1(w6_cynicism), subset_data, family=binomial)
labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age", "Other qualification", "GSCE-level", "A-level", "Higher degree", "Degree", "Education missing", "Income", "Political cynisism")
stargazer2(vote.UKIP, odd.ratio = TRUE, title="Understanding Society: Support for UKIP", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(0.05), 
           covariate.labels = labels, dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "small",column.labels = c("Base", "Figure 1"), notes = "Odds ratios with standard errors from logistic Regression models; *p<0.05",  notes.append = FALSE, out="Tables/Understanding_results.tex", no.space=TRUE, label="tab:Understanding_results" )

#calculate 1 SD above and below the mean: using subsetted ata
low  <- mean(subset_data$w3_agre, na.rm=T)-sd(subset_data$w3_agre, na.rm=T)
high <- mean(subset_data$w3_agre, na.rm=T)+sd(subset_data$w3_agre, na.rm=T)

#Odds ratio
odds.difference <- (exp(vote.UKIP[[2]]$coefficients)[2]*low) / (exp(vote.UKIP[[2]]$coefficients)[2]*high)

#SI A.1: Descriptive statistics-----
desc.labels <- c("Populist vote", labels)
stargazer(vote.UKIP[[2]]$model, covariate.labels=desc.labels, type = "latex", summary.stat = c("mean", "sd", "median","N", "min",  "max"), title="Descriptive statistics Understanding Society sample", out="Tables/Understanding_descrip.tex", no.space=TRUE, label="tab:Understanding_descriptives", digits=2)

#alpha Agreeableness
psych::alpha(data.frame(data_UK$a1, data_UK$a2, data_UK$a3))
#alpha OPenness
psych::alpha(data.frame(data_UK$o1, data_UK$o2, data_UK$o3))
#alpha Conscientiousness
psych::alpha(data.frame(data_UK$c1, data_UK$c2, data_UK$c3))
#alpha Extraversion
psych::alpha(data.frame(data_UK$e1, data_UK$e2, data_UK$e3))
#alpha Neuroticism
psych::alpha(data.frame(data_UK$n1, data_UK$n2, data_UK$n3))

#cynicism correlation 
cor.test(data_UK$w6_poleff3, data_UK$w6_poleff4)

#SI A.1: Populist support vs. vote choice --------------------
vote.UKIP.robust<-list()
vote.UKIP.robust[[1]]<- glm(attach_populist ~zero1(w3_agre) + zero1(w3_open) + zero1(w3_con)+ zero1(w3_ext) + zero1(w3_neu) +female +age + Ed_OtherQualification + Ed_GSCElevel + Ed_Alevel + Ed_higherdegree + Ed_degree + Ed_Missing + income +  zero1(w6_cynicism), data=data_UK, family=binomial)
vote.UKIP.robust[[2]]<- glm(vote_populist ~zero1(w3_agre) + zero1(w3_open) + zero1(w3_con)+ zero1(w3_ext) + zero1(w3_neu) +female +age + Ed_OtherQualification + Ed_GSCElevel + Ed_Alevel + Ed_higherdegree + Ed_degree + Ed_Missing + income +  zero1(w6_cynicism), data=data_UK, family=binomial)
stargazer2(vote.UKIP.robust, odd.ratio = TRUE, title="Understanding Society: Support for UKIP", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(0.05), covariate.labels = labels, dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "small",column.labels = c("Attach (1)", "Vote (2)"), notes = "Odds ratios with standard errors from logistic Regression models; *p<0.05",  notes.append = FALSE, out="Tables/Understanding_robust.tex", no.space=TRUE, label="tab:Understanding_robust")

#Effect size: populist attachment
low  <- mean(zero1(data_UK$w3_agre)[!is.na(data_UK$attach_populist)], na.rm=T)-sd(zero1(data_UK$w3_agre)[!is.na(data_UK$attach_populist)], na.rm=T)
high  <- mean(zero1(data_UK$w3_agre)[!is.na(data_UK$attach_populist)], na.rm=T)+sd(zero1(data_UK$w3_agre)[!is.na(data_UK$attach_populist)], na.rm=T)
odds.difference <- (exp(vote.UKIP.robust[[1]]$coefficients)[2]*low) / (exp(vote.UKIP.robust[[1]]$coefficients)[2]*high)
1/odds.difference

#Effect size: populist vote
low  <- mean(zero1(data_UK$w3_agre)[!is.na(data_UK$vote_populist)], na.rm=T)-sd(zero1(data_UK$w3_agre)[!is.na(data_UK$vote_populist)], na.rm=T)
high  <- mean(zero1(data_UK$w3_agre)[!is.na(data_UK$vote_populist)], na.rm=T)+sd(zero1(data_UK$w3_agre)[!is.na(data_UK$vote_populist)], na.rm=T)
odds.difference <- (exp(vote.UKIP.robust[[2]]$coefficients)[2]*low) / (exp(vote.UKIP.robust[[2]]$coefficients)[2]*high)
1/odds.difference
